package edu.sctu.yueding.activitys;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.adapter.LeadingViewPagerAdapter;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.httputils.SharePreference;

/**
 * Created by zcy on 2016/8/23.
 */
public class LeadingViewPagerActivity extends BaseActivity {

    private ViewPager vPager;

    private List<View> list = new ArrayList<View>();
    private LeadingViewPagerAdapter adapter;

    private ImageView[] tips;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * 去掉状态栏
         */
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);


        setContentView(R.layout.activity_leading);


        if (SharePreference.getInstance(this).ifHaveShare("isFirst")) {

            SharePreference.getInstance(this).setCache("isFirst", true);
            JumpUtils.jumpActivity(this, WelcomeActivity.class);

        }else {
            init();
        }

    }

    private void init() {

        //初始化点点点控件
        ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);
        tips = new ImageView[3];

        for (int i = 0; i < tips.length; i++) {
            ImageView imageView = new ImageView(this);

            imageView.setLayoutParams(new ViewGroup.LayoutParams(10, 10));

            if (i == 0) {
                imageView.setBackgroundResource(R.drawable.leading_selected);
            } else {
                imageView.setBackgroundResource(R.drawable.leading_un_selected);
            }
            tips[i] = imageView;

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(30, 30));

            layoutParams.leftMargin = 20;//设置点点点view的左边距
            layoutParams.rightMargin = 20;//设置点点点view的右边距
            layoutParams.bottomMargin = 40;
            group.addView(imageView, layoutParams);
        }

        vPager = (ViewPager) findViewById(R.id.leading_viewpager);


        setData();

        adapter = new LeadingViewPagerAdapter(list);
        vPager.setAdapter(adapter);
        vPager.setOffscreenPageLimit(2);
        vPager.setCurrentItem(0);
        vPager.setOnPageChangeListener(changeListener);
    }

    private void setData() {

        View first = LayoutInflater.from(this).inflate(R.layout.activity_leading_image, null);
        ImageView firstImageView = (ImageView) first.findViewById(R.id.leading_image);
        firstImageView.setImageResource(R.drawable.leading_one);

        View two = LayoutInflater.from(this).inflate(R.layout.activity_leading_image, null);
        ImageView twoImageView = (ImageView) two.findViewById(R.id.leading_image);
        twoImageView.setImageResource(R.drawable.leading_two);

        View three = LayoutInflater.from(this).inflate(R.layout.activity_leading_image, null);
        ImageView threeImageView = (ImageView) three.findViewById(R.id.leading_image);
        threeImageView.setImageResource(R.drawable.leading_three);

        ImageView imageView = (ImageView) three.findViewById(R.id.leading_image_enter);
        imageView.setImageResource(R.drawable.enter);

        list.add(first);
        list.add(two);
        list.add(three);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharePreference.getInstance(LeadingViewPagerActivity.this).setCache("isFirst", true);
                JumpUtils.jumpActivity(LeadingViewPagerActivity.this, MainActivity.class);
            }
        });



    }

    /**
     * 监听viewpager的移动
     */
    ViewPager.OnPageChangeListener changeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int index) {

            setImageBackground(index);//改变点点点的切换效果
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    };

    /**
     * 改变点点点的切换效果
     *
     * @param selectItems
     */
    private void setImageBackground(int selectItems) {
        for (int i = 0; i < tips.length; i++) {
            if (i == selectItems) {
                tips[i].setBackgroundResource(R.drawable.leading_selected);
            } else {
                tips[i].setBackgroundResource(R.drawable.leading_un_selected);
            }
        }
    }

}
